#include "ff_utils.h"
#include "ff_utils_ogl.h"

using namespace std;
using namespace feifei;
using namespace glm;


E_ffState RtnState;
int ErrorCnt = 0;
void ChkRtn()
{
    if (RtnState != E_ffState::SUCCESS)
        ErrorCnt++;
}

void Scroll(double xoffset, double yoffset)
{
    OGlHandle* gl = OGlHandle::GetOglHandle();
    Camera* camera = gl->GetCamera("default");
    camera->OnMouseScroll(yoffset);
}
void MouseButton(int button, int action, int mods)
{
    OGlHandle* gl = OGlHandle::GetOglHandle();
}
void KeyButton(int key, int scancode, int action, int mods)
{
    OGlHandle* gl = OGlHandle::GetOglHandle();
    Camera* camera = gl->GetCamera("default");
    camera->OnKeyboard(key, scancode, action, mods);
}
void CursorPos(double xpos, double ypos)
{
    OGlHandle* gl = OGlHandle::GetOglHandle();
    Camera* camera = gl->GetCamera("default");
    camera->OnMouseMove(xpos, ypos);
}

void UpdatePerDraw()
{
    OGlHandle* gl = OGlHandle::GetOglHandle();

    gl->SetCamera("default");

    // run for 5 seconds
    //if (tv > 5) gl->isQuite = true; 
}


int main()
{
    LOG("coordinate test.\n");

    OGlHandle* gl = OGlHandle::GetOglHandle();

    // 1. init opengl
    RtnState = gl->Init();     ChkRtn();
    // 2. create window
    RtnState = gl->window.CreateGlfwWindow("coordinate test", 768, 512);     ChkRtn();
    {
        gl->AddScrollCallback(Scroll);
        gl->AddMouseButtonCallback(MouseButton);
        gl->AddKeyBoardCallback(KeyButton);
        gl->AddCursorPosCallback(CursorPos);
    }
    // 3. init camera
    Camera *camera = new Camera();
    {
        camera->Reset(gl->window.Width, gl->window.Height);
        gl->AddCamera("default", camera);
        gl->SetCamera("default");
    }
    
    // setup coordinate
    BuildCoor();

    // 6. hookup pre-draw function
    gl->AddUpdatePerDrawCallback(UpdatePerDraw);

    // enter main loop
    gl->Run();

    // release
    gl->Release();
    
	return ErrorCnt;
}
